﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;

namespace NTL.DAL
{
	public abstract class CommentsProvider : DataAccess
	{
		static private CommentsProvider _instance = null;
		static public CommentsProvider Instance
		{
			get
			{
				if (_instance == null)
					_instance = (CommentsProvider)Activator.CreateInstance(
					   Type.GetType("NTL.DAL.SqlClient.SqlCommentsProvider"));
				return _instance;
			}
		}

		public CommentsProvider()
		{
			this.ConnectionString = Globals.Settings.ConnectionString;
			this.EnableCaching = Globals.Settings.EnableCaching;
			this.CacheDuration = Globals.Settings.CacheDuration;
		}

		public abstract CommentDetails FindById(long galleryId, long imageId, long commentId);
		public abstract List<CommentDetails> FindByImage(long galleryId, long imageId, long pageIndex, long pageSize);
		public abstract long Insert(long galleryId, long imageId, CommentDetails comment);
		public abstract bool Update(long galleryId, long imageId, CommentDetails comment);
		public abstract bool Delete(long galleryId, long imageId, long commentId);

		protected virtual CommentDetails GetFromReader(IDataReader reader)
		{
			return new CommentDetails(
				(long)reader["GalleryId"],
				(long)reader["ImageId"],
				(long)reader["CommentId"],
				(DateTime)reader["AddedDate"],
				reader["AddedBy"].ToString(),
				reader["Text"].ToString(),
				(bool)reader["Visible"]);
		}
		protected virtual List<CommentDetails> GetCollectionFromReader(IDataReader reader)
		{
			List<CommentDetails> comments = new List<CommentDetails>();
			while (reader.Read())
				comments.Add(GetFromReader(reader));
			return comments;
		}
	}
}
